export const getAutoRefreshOptions = () => {
  return {
    timeout: 3 * 60 * 1000,
    functionName: 'getDataAsync',
    paramsChecker: (params) => true,
  };
};

export default {
  data() {
    return {
      autoRefreshOptions: getAutoRefreshOptions(),
    };
  },
  computed: {
    apiParams() {
      return {};
    },
  },
  methods: {
    autoRefreshData() {
      const { functionName, paramsChecker, timeout } = this.autoRefreshOptions;
      if (typeof this[functionName] === 'function' && paramsChecker(this.apiParams)) {
        this[functionName](Object.assign({}, this.apiParams));
      }
      if (typeof timeout === 'number' && timeout > 0) {
        clearTimeout(this.autoRefreshTimer);
        this.autoRefreshTimer = setTimeout(() => {
          this.autoRefreshData();
        }, timeout);
      }
    },
  },
};
